/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.treeQuery;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 * @author jorgeorm
 */
public class CheckBoxRenderer extends JPanel implements TreeCellRenderer{
    protected ButtonGroup btn_grupo;
    protected JCheckBox check;
    protected TreeLabel label;
    protected Color selectionBorderColor, selectionForeground, selectionBackground, textForeground,
            textBackground;
    private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();


    public CheckBoxRenderer() {
//        setLayout(new FlowLayout());
        btn_grupo=new ButtonGroup();
        setLayout(null);
        this.check=new JCheckBox();
        this.label=new TreeLabel();
        selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
        selectionForeground = UIManager.getColor("Tree.selectionForeground");
        selectionBackground = UIManager.getColor("Tree.selectionBackground");
        textForeground = UIManager.getColor("Tree.textForeground");
        textBackground = UIManager.getColor("Tree.textBackground");
        check.setBackground(textBackground);
        label.setForeground(textForeground);
        add(label);
        add(check);
    }
    
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, 
    boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Component out=null;
            CheckBoxNode node=null;
                
            if(leaf){
//                removeAll();
                node=(CheckBoxNode) value;
//                check=node.box;
//                if(node.hasGroup())
//                {
//                    try{ 
//                        btn_grupo.remove(check);
//                    }catch(Exception excp){
//                        System.out.println("No pued sacar el box del butonGroup");
//                    }
//                    btn_grupo.add(check);
//                }
//                add(label);
//                add(check);
                String stringVal=tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
                check.setEnabled(tree.isEnabled());
                check.setSelected(node.isSelected());
                label.setIcon(UIManager.getIcon("Tree.leafIcon"));
                label.setText(stringVal);
                label.setSelected(selected);
                if(selected){
                    label.setBackground(this.selectionBackground);
                    check.setBackground(selectionBackground);
                }else{
                    label.setBackground(textBackground);
                    check.setBackground(textBackground);
                }
                label.setFont(tree.getFont());
                label.setFocus(hasFocus);
                out=this;
            }
            else{
                out=nonLeafRenderer.getTreeCellRendererComponent(tree,value, selected, expanded, leaf, row, hasFocus);
            }
            
            return out;
    }
    
    @Override
    public Dimension getPreferredSize() {
    Dimension d_check = check.getPreferredSize();
    Dimension d_label = label.getPreferredSize();
    return new Dimension(d_check.width + d_label.width,
        (d_check.height < d_label.height ? d_label.height
            : d_check.height));
  }
    
    @Override
    public void doLayout() {
        Dimension d_check = check.getPreferredSize();
        Dimension d_label = label.getPreferredSize();
        int y_check = 0;
        int y_label = 0;
        if (d_check.height < d_label.height) {
            y_check = (d_label.height - d_check.height) / 2;
        } else {
            y_label = (d_check.height - d_label.height) / 2;
        }
        check.setLocation(0, y_check);
        check.setBounds(0, y_check, d_check.width, d_check.height);
        label.setLocation(d_check.width, y_label);
        label.setBounds(d_check.width, y_label, d_label.width, d_label.height);
  }

    @Override
    public void setBackground(Color color) {
        if (color instanceof ColorUIResource)
            color = null;
        super.setBackground(color);
    }
    
}
